package com.bac.rentmap.web.tag.batch.correction;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.bac.rentmap.ad.batch.correction.CorrectionCalculator;

public class CorrectionTag extends SimpleTagSupport {
    private String key;

    @Override
    public void doTag() throws JspException, IOException {
        final ServletContext servletContext = ((PageContext) getJspContext())
                .getServletContext();
        final ApplicationContext context = WebApplicationContextUtils
                .getWebApplicationContext(servletContext);
        CorrectionCalculator correctionCalculator = (CorrectionCalculator) context.getBean("correctionCalculator");
        double res = correctionCalculator.calculate(key);
        final JspWriter writer = getJspContext().getOut();
        writer.write(String.valueOf(res));
    }

    public void setKey(String key) {
        this.key = key;
    }
    public String getKey() {
        return key;
    }
}
